/*
 * Author: yezi
 * E-mail: okye328@gmail.com
 * Create time: Dec 22, 2012 11:19:58 PM
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package com.kanbox.linux.token;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.kanbox.linux.GlobalConf;

public class Token implements Serializable {

	private static final long serialVersionUID = -4847093604737657304L;

	private static final String PATH = GlobalConf.METAPATH + "/.token";

	private String authorizationCode;
	private String access_token;
	private String refresh_token;
	// private String token_type;
	private int expire_time;// In seconds

	private long last_refresh = 0;

	public Token() {

	}

	public String getAuthorizationCode() {
		return authorizationCode;
	}

	public void setAuthorizationCode(String authorizationCode) {
		this.authorizationCode = authorizationCode;
	}

	public String getAccess_token() {
		return access_token;
	}

	public void setAccess_token(String access_token) {
		this.access_token = access_token;
	}

	public String getRefresh_token() {
		return refresh_token;
	}

	public void setRefresh_token(String refresh_token) {
		this.refresh_token = refresh_token;
	}

	// public String getToken_type() {
	// return token_type;
	// }
	//
	// public void setToken_type(String token_type) {
	// this.token_type = token_type;
	// }

	public int getExpire_time() {
		return expire_time;
	}

	public void setExpire_time(int expire_time) {
		this.expire_time = expire_time;
	}

	public long getLast_refresh() {
		return last_refresh;
	}

	public void setLast_refresh(long last_refresh) {
		this.last_refresh = last_refresh;
	}

	public void saveToFile() {

		setLast_refresh(System.currentTimeMillis());

		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(new File(PATH)));
			oos.writeObject(this);
			oos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @return False if the token is not saved.
	 */
	public boolean readFromFile() {
		File file = new File(PATH);
		if (!file.exists()) {
			return false;
		} else {
			try {
				ObjectInputStream ois = new ObjectInputStream(
						new FileInputStream(file));
				Token temp = (Token) ois.readObject();
				access_token = temp.access_token;
				refresh_token = temp.refresh_token;
				authorizationCode = temp.authorizationCode;
				expire_time = temp.expire_time;
				ois.close();
			} catch (IOException | ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return true;
		}
	}

}
